//
//  ClearData.m
//  A_Comic
//
//  Created by sos on 16/1/26.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "ClearData.h"
#import <SDImageCache.h>

@implementation ClearData
//获取单个文件的大小
+(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}
//计算目录大小
+(float)folderSizeAtPath:(NSString *)path{
    //创建管理器对象
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //获取沙河路径
    //    NSString *catch = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    CGFloat j;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *files = [fileManager subpathsAtPath:path];
        //遍历所有文件
        for (NSInteger i = 0; i < files.count; i++) {
            NSString *filePath = [path stringByAppendingPathComponent:files[i]];
            //获取文件大小
            float k = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
            j =  j + k;
            
        }
    }
    return j/1024.0/1024.0;
}
//清除缓存文件
+(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}
@end
